package com.yonyou.pmclouds.plantemplate.processor;

import com.yonyou.pmclouds.basecom.bp.processor.SingleProcessor;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.runtime.RuntimeEnvironment;
import com.yonyou.pmclouds.plantemplate.entity.ScheduleTemplateVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.UUID;

/**
 *
 * @Description: 计划模板保存前处理类
 * @Author: zhangzhonghao
 * @Date: 2020-03-06
 */

@Component
public class ScheduleTemplateInsertBeforeProcessor implements SingleProcessor<ScheduleTemplateVO> {

    @Override
    public void process(ScheduleTemplateVO vo, ScheduleTemplateVO orig) throws BusinessException {

        checkValue(vo);

        setDefault(vo);

    }

    private void checkValue(ScheduleTemplateVO vo) throws BusinessException {

        if(StringUtils.isEmpty(vo.getName())){
            throw new BusinessException("计划名称不允许为空！");
        }
    }

    private void setDefault(ScheduleTemplateVO vo) {
        String pkSchedule = UUID.randomUUID().toString();
        vo.setPkScheduleTemplate(pkSchedule);
        vo.setPkTenant(RuntimeEnvironment.getTenantId());
        vo.setCreator(RuntimeEnvironment.getLoginUserId());
        vo.setCreationtime(Calendar.getInstance().getTime());
    }

}
